package com.iotechn.unimall.app.api.equipment;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.dobbinsoft.fw.core.exception.AdminServiceException;
import com.dobbinsoft.fw.core.exception.ServiceException;
import com.dobbinsoft.fw.support.model.Page;
import com.iotechn.unimall.data.domain.ComputerRoomDO;
import com.iotechn.unimall.data.domain.EquipmentDO;
import com.iotechn.unimall.data.domain.InspectionRoundsDO;
import com.iotechn.unimall.data.enums.EquipmentStatusType;
import com.iotechn.unimall.data.exception.ExceptionDefinition;
import com.iotechn.unimall.data.mapper.ComputerRoomMapper;
import com.iotechn.unimall.data.mapper.EquipmentMapper;
import com.iotechn.unimall.data.mapper.InspectionRoundsMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;


/**
 * Generate Code By Unimall
 */
@Service
public class AppEquipmentServiceImpl implements appEquipmentService {

    @Autowired
    private EquipmentMapper equipmentMapper;
    @Autowired
    private ComputerRoomMapper computerRoomMapper;
    @Autowired
    private InspectionRoundsMapper inspectionRoundsMapper;

    @Override
    public boolean delete(Long id, Long adminId) throws ServiceException {
        //查询设备信息
        EquipmentDO equipmentDO = equipmentMapper.selectById(id);
        //更改机房设备数量
        ComputerRoomDO computerRoomDO = computerRoomMapper.selectById(equipmentDO.getRoomId());
        int i = computerRoomDO.getNumber();
        computerRoomDO.setNumber(i - 1);
        computerRoomMapper.updateById(computerRoomDO);
        return equipmentMapper.deleteById(id) > 0;
    }

    @Override
    public Page<EquipmentDO> list(String searchKeyWord, String type, String room, Integer page, Integer limit, Long adminId) throws ServiceException {
        QueryWrapper<EquipmentDO> wrapper = new QueryWrapper<EquipmentDO>();
        if (searchKeyWord != null) {
            wrapper.like("id", searchKeyWord)
                    .or().like("equipment_type", searchKeyWord)
                    .or().like("equipment_no", searchKeyWord);
        }
        if (type != null) {
            wrapper.eq("status", type);
        }
        if (room != null) {
            wrapper.eq("room_name", room);
        }
        return equipmentMapper.selectPage(Page.div(page, limit, EquipmentDO.class), wrapper.orderByDesc("id"));
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public EquipmentDO create(String equipmentType, String equipmentNo, String roomName, Long roomId, String inspectionItems, Long adminId) throws ServiceException {
        Date now = new Date();
        EquipmentDO insertDO = new EquipmentDO();
        insertDO.setEquipmentType(equipmentType);
        insertDO.setEquipmentNo(equipmentNo);
        insertDO.setRoomName(roomName);
        insertDO.setRoomId(roomId);
        insertDO.setInspectionItems(inspectionItems);
        insertDO.setStatus(EquipmentStatusType.normal.getCode());
        insertDO.setGmtUpdate(now);
        insertDO.setGmtCreate(now);
        //更改机房设备数量
        ComputerRoomDO computerRoomDO = computerRoomMapper.selectById(roomId);
        int i = computerRoomDO.getNumber();
        computerRoomDO.setNumber(i + 1);
        computerRoomMapper.updateById(computerRoomDO);
        if (equipmentMapper.insert(insertDO) > 0) {
            return insertDO;
        }
        throw new AdminServiceException(ExceptionDefinition.ADMIN_UNKNOWN_EXCEPTION);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public String edit(Long id, String equipmentType, String equipmentNo, String roomName, Long roomId, String inspectionItems, Long adminId) throws ServiceException {
        Date now = new Date();
        EquipmentDO updateDO = new EquipmentDO();
        updateDO.setId(id);
        updateDO.setEquipmentType(equipmentType);
        updateDO.setEquipmentNo(equipmentNo);
        updateDO.setRoomName(roomName);
        updateDO.setRoomId(roomId);
        updateDO.setInspectionItems(inspectionItems);
        updateDO.setGmtUpdate(now);
        if (equipmentMapper.updateById(updateDO) > 0) {
            return "ok";
        }
        throw new AdminServiceException(ExceptionDefinition.ADMIN_UNKNOWN_EXCEPTION);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public String discard(Long id, String flag, Long adminId) throws ServiceException {
        Date now = new Date();
        EquipmentDO equipmentDO = equipmentMapper.selectById(id);
        if ("1".equals(flag)) {
            //启用
            equipmentDO.setStatus(EquipmentStatusType.normal.getCode());
        } else {
            //弃用
            equipmentDO.setStatus(EquipmentStatusType.discard.getCode());
        }
        equipmentDO.setGmtUpdate(now);
        if (equipmentMapper.updateById(equipmentDO) > 0) {
            return "ok";
        }
        throw new AdminServiceException(ExceptionDefinition.ADMIN_UNKNOWN_EXCEPTION);
    }

    @Override
    public List<EquipmentDO> xiaList(String roomName, String equipmentType) throws ServiceException {
        if (equipmentType == null) {
            List<EquipmentDO> equipmentDOS = equipmentMapper.selectList(new QueryWrapper<EquipmentDO>().eq("room_name", roomName));
            equipmentDOS = equipmentDOS.stream().distinct().collect(Collectors.toList());
            return equipmentDOS;
        } else {
            List<EquipmentDO> equipmentDOS = equipmentMapper.selectList(new QueryWrapper<EquipmentDO>().eq("room_name", roomName)
                    .eq("equipment_type", equipmentType));
            equipmentDOS = equipmentDOS.stream().distinct().collect(Collectors.toList());
            return equipmentDOS;
        }
    }

    @Override
    public List<EquipmentDO> allEquipment(String roomName) throws ServiceException {
        //获取当前年月日
        SimpleDateFormat f = new SimpleDateFormat("yyyy-MM-dd");//设置日期格式
        String date = f.format(new Date(System.currentTimeMillis()));
        List<EquipmentDO> equipmentDOS = equipmentMapper.selectList(new QueryWrapper<EquipmentDO>().eq("room_name", roomName).ne("status","3"));
        //查询轮数
        List<InspectionRoundsDO> inspectionRoundsDOS = inspectionRoundsMapper.selectList(new QueryWrapper<InspectionRoundsDO>()
                .like("inspection_start", date).orderByDesc("rounds"));
        if (!CollectionUtils.isEmpty(inspectionRoundsDOS)){
            if (!CollectionUtils.isEmpty(equipmentDOS)) {
                for (EquipmentDO equipmentDO : equipmentDOS) {
                    equipmentDO.setRoundsId(inspectionRoundsDOS.get(0).getId());
                    equipmentDO.setQrCode(true);
                }
            }
        }
        return equipmentDOS;
    }

}
